﻿using UnityEngine;
using System.Collections;

namespace MainModul
{
    public class MapPlayer : MonoBehaviour
    {
        public GameObject manager;

        Transform destination;
        float startTime, speed, distance;
        bool move;
        // Use this for initialization
        void Start()
        {
            move = false;
            speed = 0.1f;
        }

        // Update is called once per frame
        void Update()
        {
            if (move)
            {
                float progress = ((Time.time - startTime) * speed) / distance;
                transform.position = Vector2.Lerp(transform.position, destination.position, progress);

                //if (transform.position == destination.position)
                //{
                //    EnterDungeon(destination.name);
                //}
            }
        }

        void Move(string targetName)
        {
            //Debug.Log(targetName);
            destination = GameObject.Find(targetName).transform;
            move = true;
            startTime = Time.time;
            distance = Vector2.Distance(transform.position, destination.position);
        }

        void OnCollisionEnter2D(Collision2D collision)
        {
            if (collision.collider.tag == "Node")
            {
                Debug.Log("finish");
                move = false;
            }
        }

        void EnterDungeon(string dungeonName)
        {
            Application.LoadLevel(dungeonName);
        }
    }
}